/******************************************************************************
 * Copyright (C) 2016 Wuhan Water Elephant Co.Ltd All Rights Reserved. 
 * 本软件为武汉水象科技有限公司开发研制。 未经本公司正式书面同意，其他任何个人、
 * 团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package com.sxfq.loansupermarket.app.common.controller;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sxfq.loansupermarket.common.annotation.LoginIntercept;
import com.sxfq.loansupermarket.common.constants.RedisKeyConstants;
import com.sxfq.loansupermarket.common.utils.ControllerUtil;
import com.sxfq.loansupermarket.common.utils.RedisUtil;
import com.sxfq.loansupermarket.common.utils.VerifyCodeUtils;

/**
 * 
 * 
 * Module:
 * 
 * SmsController.java
 * 
 * @author 毛恩奇
 * @since JDK 1.8
 * @version 1.0
 * @description: <描述>
 */
@Controller
@RequestMapping("/img")
@LoginIntercept(checkLogin = false)
public class ImgController {
	@Value("${img.verify.seconds:300}")
	private int verifySeconds;

	/**
	 * 获取验证码图片
	 */
	@RequestMapping(value = "/getVerifyCode")
	public void getVerifyCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String businessToken = ControllerUtil.getOrCreateBusinessToken(request, response);
		response.setContentType("image/jpeg");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		String verifyCode = RandomStringUtils.randomNumeric(4);
		RedisUtil.setex(RedisKeyConstants.IMG_VERIFY_CODE_PRE + businessToken.hashCode(), verifyCode, verifySeconds);
		ServletOutputStream os = response.getOutputStream();
		VerifyCodeUtils.outputImage(116, 43, os, verifyCode);
		os.flush();
		os.close();
	}
}